﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnitOfWork;
using EMC.Service.DAL;
namespace EMC.Service.Biz.Common
{
    /// <summary>
    /// infocode:accountdoesnotexist,passwordiswrong,
    /// </summary>
    public class AuthorizationBiz : Biz
    {
        public IUnitOfWork UoW;

        public AuthorizationBiz()
        {
            IsOk = true;
            UoW = new EFUnitOfWork<EMC.Service.DAL.EMCEntities>();


        }
        public void NewAccount(string account, string password)
        {
            if (account == string.Empty && password == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "AccountAndPasswordIsEmpty";
                this.InfoMsg = "账户和密码为空";
                return;
            }
            if (account == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "AccountStringIsEmpty";
                this.InfoMsg = "账户为空";
                return;
            }
            if (password == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "PasswordIsEmpty";
                this.InfoMsg = "密码为空";
                return;
            }
            if (AccountIsExist(account))
            {
                this.IsOk = false;
                InfoCode = "AccountIsExist";
                this.InfoMsg = "账户已经存在";
                return;
            }
            Common_Account acct = new Common_Account() { AccountName = account.Trim(), Id = Guid.NewGuid(), Password = password };
            UoW.Insert<Common_Account>(acct);
            UoW.SaveChanges(true);

        }
        public Common_Account ValidateAccount(string account, string password)
        {
            if (account == string.Empty && password == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "AccountAndPasswordIsEmpty";
                this.InfoMsg = "账户和密码不能为空";
                return null;
            }
            if (account == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "AccountStringIsEmpty";
                this.InfoMsg = "账户不能为空";
                return null;
            }
            if (password == string.Empty)
            {
                this.IsOk = false;
                InfoCode = "PasswordIsEmpty";
                this.InfoMsg = "密码不能为空";
                return null;
            }
            if (!AccountIsExist(account))
            {
                this.IsOk = false;
                InfoCode = "AccountIsWrong";
                this.InfoMsg = "账户错误";
                return null;
            }
            var acct = UoW.Get<Common_Account>(a => a.AccountName == account).FirstOrDefault();
            
            if (acct.Password == password)
            {
                return acct;
            }
            else
            {

                this.IsOk = false;
                InfoCode = "PasswordIsWrong";
                this.InfoMsg = "密码错误";
                return acct;
            }
        }
        public void LockAccount(string account)
        {

        }
        public bool AccountIsExist(string account)
        {
            var count = UoW.Get<Common_Account>().Where(a => a.AccountName == account.Trim()).Count();
            if (count == 0)
                return false;
            else
                return true;
        }

    }
}
